<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/21
 * Time: 9:34
 */

namespace app\index\controller;

use app\index\server\BannerItemServer;
use app\index\validate\BannerItemValidate;
use app\index\validate\BannertoItemValidate;
use app\index\validate\GetListValidate;
use app\index\model\Banner as BannerModel;
use app\index\validate\IDMustBePostiveInt;
use think\Exception;

class BannerItem extends Auth
{
    protected $server;

    public function __construct()
    {
        $this->server = new BannerItemServer();
    }

    public function getBannerItemList()
    {
        // 验证 成功返回 params
        $params = (new GetListValidate())->goCheck();
        // 操作
        $result = $this->server->getList($params);
        // return
        return $result;
    }

    public function addOrUpdate()
    {
        // 验证传入的数据只获取需要的数据
        $params = (new BannerItemValidate())->goCheck();
        // 操作
        $result = $this->server->addOrUpdate($params);
        // return
        return $result;
    }

    public function detail($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $result = $this->server->detail($id);
        return $result;
    }


    public function changeStatus($id)
    {
        (new IDMustBePostiveInt())->goCheck();
        $result = $this->server->changeStatus($id);
        return $result;
    }

    // 对option进行填充
    public function fill()
    {
        try {
            $fill = BannerModel::where('status', '=', 1)
                ->field('name, id')
                ->select();
            if(!$fill) {
                return setMsg(0, '没有可用Banner');
            }
        } catch (Exception $e) {
            return setMsg(0, $e->getMessage());
        }
        return setMsg(1, '查找Banner成功', $fill);
    }
}